#ifndef CUSTOM_FOG_INCLUDE
#define CUSTOM_FOG_INCLUDE


 
	//#define  PIXEL_FOG 1
	#include "UnityLightingCommon.cginc"
	half4 _FogColor;
 
	half4 _HeightFogParam;
	/*
		_HeightFogParam.x = _HeightFogDensity;
        _HeightFogParam.y = _FogHeightFallOff;
        _HeightFogParam.z = _FogHeight;
        _HeightFogParam.w = _FogStartDis;
	*/
	//rsqrt(dot(v,v))*v;
	 
	#define MATH_E 2.71828182846
	 
	float CmpHeightFogFactor(float3 worldPos )
	{

		float3 dir =  _WorldSpaceCameraPos.xyz - worldPos.xyz  ;
		float distance = length(dir);
		dir = dir/distance;
		
		if(distance<_HeightFogParam.w)
			return 0;
		float3 begin = _WorldSpaceCameraPos.xyz - dir * _HeightFogParam.w;
		float b = _HeightFogParam.y*(begin.y-_HeightFogParam.z);
		float a = _HeightFogParam.y*(worldPos.y-_HeightFogParam.z);

		float delta = pow(MATH_E, -a) -pow(MATH_E,-b);

		float density = exp(-b);
		density+=delta;
		distance -= _HeightFogParam.w;
		float fogFactor = saturate(  distance *_HeightFogParam.x * density)*unity_FogColor.a ;
		return fogFactor;

	}

	#define LCH_FOG_COLOR  unity_FogColor

	#define EFFECT_FOG_COLOR _BlendMode>0? float4(0,0,0,1):LCH_FOG_COLOR
 
	#ifdef PIXEL_FOG
		#ifdef LCH_HEIGHT_FOG
			#define LCH_FOG_COORDS(ID) //float _fogCoord : TEXCOORD##ID;
			#define LCH_TRANSFER_FOG(v2f, worldPos,pos)

			#define LCH_APPLY_FOG_EFFECT(v2f, pixelColor,worldPos) float _fogCoord = CmpHeightFogFactor(worldPos )   ;  pixelColor.rgb = lerp(pixelColor.xyz,EFFECT_FOG_COLOR.rgb, _fogCoord)
			#ifdef  TERRAIN_ADDPASS
				#define LCH_APPLY_FOG(v2f, pixelColor,worldPos) float _fogCoord = CmpHeightFogFactor(worldPos )   ;  pixelColor.rgb = lerp(pixelColor.xyz,0.0, _fogCoord);
			#else
				#define LCH_APPLY_FOG(v2f, pixelColor,worldPos) float _fogCoord = CmpHeightFogFactor(worldPos )   ;  pixelColor.rgb = lerp(pixelColor.xyz,LCH_FOG_COLOR.rgb, _fogCoord)  ;  
			#endif //TERRAIN_ADDPASS
			
		#else

			#define LCH_FOG_COORDS(ID)  UNITY_FOG_COORDS(ID)
			#define LCH_TRANSFER_FOG(v2f, worldPos,pos) UNITY_TRANSFER_FOG(v2f,pos);
			#define LCH_APPLY_FOG_EFFECT(v2f, pixelColor,worldPos)  UNITY_APPLY_FOG_COLOR(v2f.fogCoord, pixelColor,EFFECT_FOG_COLOR);
			#ifdef  TERRAIN_ADDPASS
				#define LCH_APPLY_FOG(v2f, pixelColor,worldPos) UNITY_APPLY_FOG_COLOR(v2f.fogCoord, pixelColor, fixed4(0,0,0,0));
			#else
				#define LCH_APPLY_FOG(v2f, pixelColor,worldPos) UNITY_APPLY_FOG_COLOR(v2f.fogCoord, pixelColor,LCH_FOG_COLOR);
			#endif //TERRAIN_ADDPASS
			
		#endif //LCH_HEIGHT_FOG

	#else //PIXEL_FOG

		#ifdef LCH_HEIGHT_FOG
			#define LCH_FOG_COORDS(ID) float _fogCoord : TEXCOORD##ID;
			#define LCH_TRANSFER_FOG(v2f, worldPos,pos) v2f##._fogCoord = CmpHeightFogFactor(worldPos)

			#ifdef  TERRAIN_ADDPASS
				#define LCH_APPLY_FOG(v2f, pixelColor,worldPos) pixelColor.rgb = lerp(pixelColor.xyz,0.0,v2f##._fogCoord);
			#else 
				#define LCH_APPLY_FOG(v2f, pixelColor,worldPos) pixelColor.rgb = lerp(pixelColor.xyz,LCH_FOG_COLOR.rgb,v2f##._fogCoord);
			#endif //TERRAIN_ADDPASS
			//#define LCH_APPLY_FOG_EFFECT(v2f, pixelColor,worldPos)    pixelColor.rgb = lerp(pixelColor.xyz,_BlendMode>0? fixed3(0,0,0),LCH_FOG_COLOR.rgb,v2f##._fogCoord);  
			#define LCH_APPLY_FOG_EFFECT(v2f, pixelColor,worldPos)    pixelColor.rgb = lerp(pixelColor.xyz,EFFECT_FOG_COLOR.rgb, v2f##._fogCoord)
		#else //LCH_HEIGHT_FOG

			#define LCH_FOG_COORDS(ID)  UNITY_FOG_COORDS(ID)
			#define LCH_TRANSFER_FOG(v2f, worldPos,pos) UNITY_TRANSFER_FOG(v2f,pos);
			#define LCH_APPLY_FOG_EFFECT(v2f, pixelColor,worldPos) UNITY_APPLY_FOG_COLOR(v2f.fogCoord, pixelColor, EFFECT_FOG_COLOR);
	   
			#if  TERRAIN_ADDPASS
					#define LCH_APPLY_FOG(v2f, pixelColor,worldPos) UNITY_APPLY_FOG_COLOR(v2f.fogCoord, pixelColor, fixed4(0,0,0,0));
			#else 
				#define LCH_APPLY_FOG(v2f, pixelColor,worldPos) UNITY_APPLY_FOG_COLOR(v2f.fogCoord, pixelColor,LCH_FOG_COLOR);
			#endif //TERRAIN_ADDPASS
		#endif //LCH_HEIGHT_FOG
    #endif //PIXEL_FOG

	 
#endif //CUSTOM_FOG_INCLUDE